var preBuyNum = 0;
$(function() {
			// 减少数量
			$("input.btn_cart[id^='un_']").click(function() {
						var num = parseInt($(this).next(".text_cart").val());
						if (num > 1) {
							num--;
							$(this).next(".text_cart").val(num)
							modifyShShoppingCart($(this).next(".text_cart").attr("id"), num);
						} else if (num == 1) {
							$(this).attr("disabled", true);
							showTipDialog("不能继续减少购买数量", $(this)[0]);
						}
					});

			// 增加数量
			$("input.btn_cart[id^='up_']").click(function() {
						var num = parseInt($(this).prev(".text_cart").val());
						if (num < 9) {
							num++;
							$(this).prev(".text_cart").val(num)
							modifyShShoppingCart($(this).prev(".text_cart").attr("id"), num);
						} else {
							$(this).attr("disabled", true);
							showTipDialog("最多可购买9件", $(this)[0]);
						}
					});

			// 数量焦点
			$("input.text_cart").focus(function() {
						preBuyNum = $(this).val();
					});

			// 数量修改
			$("input.text_cart").change(function() {
						var newBuyNum = $(this).val();
						if (!newBuyNum || isNaN(newBuyNum)) {
							$(this).val(preBuyNum);
						} else {
							if (newBuyNum > 9 || newBuyNum < 1) {
								$(this).val(preBuyNum);
								showTipDialog("请输入1～9之间的数字", $(this)[0]);
							} else {
								modifyShShoppingCart($(this).attr("id"), newBuyNum);
							}
						}
					});

			// 删除
			$(".i_e[id^='del_']").click(function() {
						var idVal = $(this).attr("id");
						var list = art.dialog.list;
						for (var i in list) {
							list[i].close();
						}
						art.dialog({
									icon : "question",
									title : "提示",
									content : "确定要删除吗？",
									drag : true,
									lock : true,
									fixed : true,
									ok : function() {
										var pNo = idVal.substr(4);
										$.post("cartUser_delete.action", {
													itemId : Number(pNo)
												}, function(data) {
												 var d = eval("("+data+")");
													if (d.status) {
														loadShoppingCart();
													} else {
														alertWarnMsgDialog(d.message);
													}
												});
									},
									cancel : true
								});
					});

			// 清空购物车
			$("#btn_clear_cart").click(function() {
						var list = art.dialog.list;
						for (var i in list) {
							list[i].close();
						}
						art.dialog({
									icon : "question",
									title : "提示",
									content : "您确定清空购物车里面的所有物品吗？",
									drag : true,
									lock : true,
									fixed : true,
									okVal : "清空",
									ok : function() {
										$.post("mall/cart/clear", {}, function(data) {
													if (data.result) {
														loadShoppingCart();
													} else {
														alertWarnMsgDialog(data.message);
													}
												});
									},
									cancelVal : "不清空",
									cancel : true
								});
					});
		});

// 异步更新购物车
function modifyShShoppingCart(productNo, newBuyNum) {
	//$.post("mall/cart/modify", {
	//			productNo : productNo,
	//			productNum : newBuyNum
	//		}, function(data) {
	//			if (data.result) {
	//				loadShoppingCart();
	//			}
	//		});
}

function showTipDialog(content, followEle) {
	var list = art.dialog.list;
	for (var i in list) {
		list[i].close();
	}
	art.dialog({
				follow : followEle,
				title : false,
				content : content,
				drag : false,
				ok : false,
				cancel : false
			}).time(5);
}


function toSubmit(){
	var ids = "";
	$("input[name='itemId']").each(function(i){
		ids = ids + ","+this.value ; 
	});
	$("#itemIds").val(ids);
	$("#orderForm").submit();
}


